package com.padakeji.android.ui.examples;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.padakeji.android.ui.widget.AutoWrapLayoutManager;
import com.padakeji.android.ui.widget.FullyLinearLayoutManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by atanl on 2016-01-28.
 */
public class AutoWrapperLayoutTest2Activity extends Activity {

    // Views.
    @Bind(R.id.autoWrapperLayoutTest2_recycler_outer)
    RecyclerView outerRecycler;

    List<List<String>> data = new ArrayList<>();
    private OuterAdapter outerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autowrapperlayout_test2);
        ButterKnife.bind(this);

        configViews();
    }

    private void configViews() {
        outerAdapter = new OuterAdapter(this);
        outerRecycler.setLayoutManager(new FullyLinearLayoutManager(this));
        outerRecycler.setAdapter(outerAdapter);

        data = Arrays.asList(
                Arrays.asList(new String[]{"1a", "1b", "1c",}),
                Arrays.asList(new String[]{"2a", "2b", "2c",}),
                Arrays.asList(new String[]{"3a", "3b", "3c",})
        );
        outerAdapter.setData(data);
    }


    private static class OuterAdapter extends RecyclerView.Adapter {

        private Context mContext;
        private LayoutInflater mInflater;

        private List<List<String>> data;

        private OuterAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
            data = new ArrayList<>();
        }

        public void setData(List<List<String>> data) {
            this.data.clear();
            if (data != null) {
                this.data.addAll(data);
            }
            notifyDataSetChanged();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = mInflater.inflate(R.layout.autowrapperlayout_test2_outer, null, false);
            return new OuterViewHolder(v);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            OuterViewHolder vh = (OuterViewHolder) holder;

            Object tag = vh.contentWrapper.getTag();
            InnerViewHolder innerViewHolder;
            if (tag == null) {
                innerViewHolder = new InnerViewHolder();
                View v = mInflater.inflate(R.layout.autowrapperlayout_test2_inner, null, false);
                vh.contentWrapper.addView(v);
                innerViewHolder.innerRecycler = (RecyclerView) v.findViewById(R.id.autoWrapperLayoutTest2_recycler_tag);
                innerViewHolder.innerRecycler.setLayoutManager(new AutoWrapLayoutManager(mContext, true));
                innerViewHolder.innerAdapter = new InnerAdapter(mContext);
                innerViewHolder.innerRecycler.setAdapter(innerViewHolder.innerAdapter);

                vh.contentWrapper.setTag(innerViewHolder);
            } else {
                innerViewHolder = (InnerViewHolder) tag;
            }

            List<String> item = data.get(position);
            innerViewHolder.innerAdapter.setData(item);

        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        private class OuterViewHolder extends RecyclerView.ViewHolder {
            FrameLayout contentWrapper;

            public OuterViewHolder(View itemView) {
                super(itemView);
                contentWrapper = (FrameLayout) itemView.findViewById(R.id.autoWrapperLayoutTest2_recycler_inner);
            }
        }

        private class InnerViewHolder {
            RecyclerView innerRecycler;
            InnerAdapter innerAdapter;
        }

    }

    private static class InnerAdapter extends RecyclerView.Adapter {

        private Context mContext;
        private LayoutInflater mInflater;
        private List<String> data;

        private InnerAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
            data = new ArrayList<>();
        }

        public void setData(List<String> data) {
            this.data.clear();
            if (data != null) {
                this.data.addAll(data);
            }
            notifyDataSetChanged();
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = mInflater.inflate(R.layout.autowrapperlayout_test2_inner_item, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh = (ViewHolder) holder;
            vh.tagText.setImageDrawable(mContext.getResources().getDrawable(R.drawable.fuckyou));
        }


        @Override
        public int getItemCount() {
            return data.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder {

            ImageView tagText;

            public ViewHolder(View itemView) {
                super(itemView);
                tagText = (ImageView) itemView.findViewById(R.id.autoWrapperLayoutTest2_text_fuck);
            }


        }
    }


}
